package com.example.mapper;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.example.model.UserRegisterDTO;
import com.example.model.entity.User;
import com.example.model.req.AddEmployeeReq;
import com.example.model.resp.ListAdminEmployeeResp;
import com.example.model.resp.ListEmloyeeInfoResp;

@Mapper
public interface UserMapper {
        @Select("select id, phone, password from tb_user where phone = #{phone}")
        User getLoginTicketByPhone(@Param("phone") String phone);

        @Insert("insert into tb_user(openId, avatar, phone, username, password, createAt) values(#{openId}, #{avatar}, #{phone}, #{username}, #{password}, CURRENT_TIMESTAMP)")
        @Options(useGeneratedKeys = true, keyProperty = "id")
        int userRegister(UserRegisterDTO userRegisterDTO);

        @Select("select id, password from tb_user where username = #{username}")
        User getAdminLoginTicket(@Param("username") String username);

        @Insert("insert into tb_user(username, password, gender, phone, price, createAt) values(#{employeeName}, 'fa5473530e4d1a5a1e1eb53d2fedb10c', #{gender}, #{phone}, #{price}, CURRENT_TIMESTAMP)")
        @Options(useGeneratedKeys = true, keyProperty = "id")
        int addEmployee(AddEmployeeReq employeeReq);

        @Select({
                        "<script>",
                        "SELECT id, username, avatar, gender, price, createAt FROM tb_user WHERE id IN ",
                        "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
                        "#{id}",
                        "</foreach>",
                        "</script>"
        })
        List<User> listEmployeeInMiniByIds(@Param("ids") List<Integer> ids);

        @Select({
                        "<script>",
                        "SELECT id, username, avatar, gender, price, createAt FROM tb_user WHERE id IN ",
                        "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
                        "#{id}",
                        "</foreach>",
                        "</script>"
        })
        List<User> listEmployeeByIds(@Param("ids") List<Integer> ids);

        @Select("select username from tb_user where id = #{id}")
        String getUsernameById(@Param("id") Integer id);

        @Select("select balance from tb_user where id = #{id}")
        BigDecimal getBalanceById(@Param("id") Integer id);

        @Select("select price from tb_user where id = #{id}")
        BigDecimal getPriceById(@Param("id") Integer id);

        @Update("update tb_user set balance = #{balance} where id = #{id}")
        int setBalanceById(@Param("id") Integer id, @Param("balance") BigDecimal balance);

        @Select("SELECT u.id, u.username, u.avatar, u.balance, u.gender FROM tb_user u WHERE u.openId = #{openId}")
        User detailByOpenId(@Param("openId") String openId);

        @Select("SELECT u.id, u.username, u.avatar, u.gender FROM tb_user u WHERE u.id = #{id}")
        User detailByUserId(@Param("id") Integer id);

        @Select("select id from tb_user where openId = #{openId}")
        int getIdByOpenId(@Param("openId") String openId);

        @Update("update tb_user set avatar = #{avatar} where openId = #{openId}")
        int updateAvatar(@Param("openId") String openId, @Param("avatar") String avatar);

        @Update("update tb_user set gender = #{gender} where openId = #{openId}")
        int updateGender(@Param("openId") String openId, @Param("gender") boolean gender);

        @Select("select gender from tb_user where openId = #{openId}")
        int getGenderByOpenId(@Param("openId") String openId);

        @Select("select username as employeeName, avatar, gender, price from tb_user where id = #{employeeId}")
        ListEmloyeeInfoResp employeeeDetail(@Param("employeeId") Integer employeeId);

        @Select("select phone from tb_user where id = #{id}")
        String getPhoneById(@Param("id") Integer id);

        @Select("select avatar from tb_user where id = #{id}")
        String getUserAvatarById(@Param("id") Integer id);

        @Select("select s.serviceName, u.id, u.username as employeeName, u.avatar, u.gender, u.price from tb_user u join con_employee_service ces on ces.userId = u.id join tb_service s on s.id = ces.serviceId limit #{start}, #{pageSize}")
        List<ListAdminEmployeeResp> listAdminEmployee(@Param("start") Integer start,
                        @Param("pageSize") Integer pageSize);

        @Update("update tb_user set price = #{price} where id = #{id}")
        int updatePrice(@Param("price") BigDecimal price, @Param("id") Integer id);

        @Delete("delete from tb_user where id = #{id}")
        int deleteById(@Param("id") Integer id);

        @Update("update tb_user set balance = balance + #{chargeVal} where openId = #{openId}")
        int chargeBalance(@Param("openId") String openId, @Param("chargeVal") BigDecimal chargeVal);

        @Select("select balance from tb_user where openId = #{openId}")
        BigDecimal queryBalance(@Param("openId") String openId);
}